<template>
    <section>
        <el-row>
            <el-col :span="24">
                <el-table :data="list" @selection-change="select">
                    <el-table-column
                        type="selection"
                        width="55">
                    </el-table-column>
                    <el-table-column prop="id" label="id" width="40">
                    </el-table-column>
                    <el-table-column prop="name" label="用户名" min-width="120">
                    </el-table-column>
                    <el-table-column prop="desc" label="备注" min-width="120">
                    </el-table-column>
                    <el-table-column prop="email" label="邮箱" width="120">
                    </el-table-column>
                    <el-table-column prop="created_at" label="创建时间" width="140">
                    </el-table-column>
                    <el-table-column label="操作" min-width="120">
                        <template slot-scope="scope">
                            <el-button @click="edit(scope.row)">修改</el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </el-col>
        </el-row>

        <div style="padding-top: 10px">
            <el-button type="primary" @click="edit">新增</el-button>
            <el-button type="danger" @click="del">删除</el-button>
        </div>

        <el-dialog
            title="编辑信息"
            :visible.sync="addFormVisible"
            width="30%">
            <el-form :model="admin" label-width="80" :rules="rule" ref="admin">
                <el-form-item label="输入邮箱" prop="email">
                    <el-input v-model="admin.email"></el-input>
                </el-form-item>
                <el-form-item label="输入密码" prop="password">
                    <el-input v-model="admin.password"></el-input>
                </el-form-item>
                <el-form-item label="确认密码" prop="passwordConfirm">
                    <el-input v-model="admin.passwordConfirm"></el-input>
                </el-form-item>
                <el-form-item label="用户名" prop="name">
                    <el-input v-model="admin.name"></el-input>
                </el-form-item>
                <el-form-item label="备注" prop="desc">
                    <el-input v-model="admin.desc"></el-input>
                </el-form-item>
            </el-form>
            <div style="text-align: center">
                <el-button @click="addFormVisible = false">关闭</el-button>
                <el-button @click="Editsubmit">提交</el-button>
            </div>
        </el-dialog>
    </section>
</template>
<script>
    /* eslint-disable indent,space-before-function-paren,spaced-comment */
    import {deleteAdmin, getAdmin, updateAdmin, addAdmin} from '../../api/api'

    export default {
        data() {
            var validatePass = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请再次输入密码'))
                } else if (value !== this.admin.password) {
                    callback(new Error('两次输入密码不一致!'))
                } else {
                    callback()
                }
            }
            return {
                list: [],
                dialogVisible: false,
                addFormVisible: false,
                current: {},
                currentSelect: [],
                admin: {},
                rule: {
                    name: [
                        {required: true, message: '请输入用户名', trigger: 'blur'}
                    ],
                    email: [
                        {required: true, message: '请输入邮箱', trigger: 'blur'}
                    ],
                    password: [
                        {required: true, message: '请输入密码', trigger: 'blur'}
                    ],
                    passwordConfirm: [
                        {required: true, message: '密码不一致', trigger: 'blur'},
                        {validator: validatePass, trigger: 'blur'}
                    ]
                }
            }
        },
        methods: {
            Init() {
                getAdmin().then(
                    data => {
                        if (data.status) {
                            this.list = data.data
                        } else {
                            this.$message.error(data.msg)
                        }
                    }
                ).catch()
            },
            del() {
                this.$confirm('此操作将永久删除该, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    deleteAdmin({
                        id: this.currentSelect.map(x => x.id).join(',')
                    }).then(
                        data => {
                            if (data.status) {
                                this.$message.success('删除成功')
                                this.Init()
                            } else {
                                this.$message.error(data.msg)
                            }
                        }
                    ).catch()
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    })
                })
            },
            select(row) {
                this.currentSelect = row
            },
            edit(a = false) {
                if (a) {
                    this.admin = {
                        id: a.id,
                        name: a.name,
                        email: a.email,
                        desc: a.desc,
                        password: ''
                    }
                } else {
                    this.admin = {
                        name: '',
                        email: '',
                        desc: '',
                        password: ''
                    }
                }
                this.addFormVisible = true
            },
            Editsubmit() {
                this.$refs['admin'].validate((valid) => {
                    if (valid) {
                        var para = {
                            ...this.admin
                        }
                        if (para.id) {
                            updateAdmin(para).then(
                                data => {
                                    this.Init()
                                    this.addFormVisible = false
                                }
                            ).catch()
                        } else {
                            addAdmin(para).then(
                                data => {
                                    this.Init()
                                    this.addFormVisible = false
                                }
                            ).catch()
                        }
                    } else {
                        console.log('error submit!!')
                        return false
                    }
                })
            }
        },
        mounted() {
            this.Init()
        }
    }
</script>
<style scoped>
</style>
